#
# File: handler_model.py
# Description: The model for our custom Request Handler
#
# Copyright 2013 Adam Meadows
#
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.
#

from django.utils.translation import ugettext as _
from django.shortcuts import render

class RequestHandler(object):
    '''An HTTP Request handler that displays a view'''

    GAE_BASE = 'https://developers.google.com/appengine'
    URLS = {
        'css': '/resources/css',
        'js': '/resources/js',
        'gae_img': '%s/images/appengine-silver-120x30.gif' % GAE_BASE,
        'cc_license': 'http://creativecommons.org/licenses/by/3.0/',
    }

    template = ''
    template_kwargs = {}

    def __init__(self, request, template=None):
        cls = self.__class__
        self.request = request
        self.template = template or cls.template
        self.template_kwargs = {}
        self.template_kwargs.update(cls.template_kwargs)

    def get(self):
        '''Default GET handler'''
        return self.render_template()

    def post(self):
        '''Default POST handler'''
        return self.render_template()

    def render_template(self, template=None, **kwargs):
        kwargs.update(self.URLS)
        kwargs.update(self.template_kwargs)
        kwargs.setdefault("user", self.request.user)
        return render(self.request, self.template, dictionary=kwargs)

    def get_response(self):
        '''Return a response to the request we were given'''
        if self.request.method == 'GET':
            return self.get()
        elif self.request.method == 'POST':
            return self.post()

